/*
 * @Descripttion:
 * @Author: chunhu-yin
 * @Date: 2021-03-17 16:00:42
 * @LastEditTime: 2021-03-18 19:51:48
 */
import axios from 'axios'
import qs from 'qs'
import Auth from './../utils/auth'

const REACT_APP_ENV = process.env.REACT_APP_ENV
if (REACT_APP_ENV === 'production') {
    axios.defaults.baseURL = 'http://prod.bang.com/' //根据项目自己更改
} else if (REACT_APP_ENV === 'test') {
    axios.defaults.baseURL = 'http://test.bang.com/' //根据项目自己更改
} else {
    axios.defaults.baseURL = 'http://127.0.0.1:8091/' //根据项目自己更改
}
//一些配置，发起请求和响应可以打印出来查看
axios.interceptors.request.use((config) => {
    //如果项目中有将token绑定在请求数据的头部，服务器可以有选择的返回数据，只对有效的请求返回数据，这样写
    //这里是用户登录的时候，将token写入了sessionStorage中了，之后进行其他的接口操作时，进行身份验证。
    if (config.url !== '/tour/auth/login') {
        config.headers.Authorization = Auth.getToken();
    }
    return config;
})
//在response中
axios.interceptors.response.use(config => {
    // console.log(config)
    return config;
})

const http = {
    post: '',
    get: '',
    put: '',
    del: ''
}

http.post = function (api, data) {
    return new Promise((resolve, reject) => {
        axios.post(api, data).then(response => {
            resolve(response.data)
        })
    })
}

http.get = function (api, data) {
    return new Promise((resolve, reject) => {
        axios.get(api, { params: data }).then(response => {
            resolve(response.data)
        })
    })
}

http.delete = function (api, data) {
    return new Promise((resolve, reject) => {
        axios.delete(api, data).then(response => {
            resolve(response.data)
        })
    })
}

http.put = function (api, data) {
    return new Promise((resolve, reject) => {
        axios.put(api, data).then(response => {
            resolve(response.data)
        })
    })
}

export default http